<?php
//Start session
require_once '../library/config.php';
require_once '../library/database.php';
/*
 * comment here
 */
function resourceLang($key ){
	$GLOBALS['curtlang']==""?$get_current_lang='en-US':$get_current_lang=$GLOBALS['curtlang'];
	$pathFile=$_SERVER['REQUEST_URI'];
	//Create query
	$qry="SELECT `res_value` FROM `tbl_resource_language` WHERE `res_key`='$key' and `resource_code`='$get_current_lang'";
	$result=mysql_query($qry);
	if(mysql_num_rows($result) == 1)
	{
		$langValue = mysql_fetch_assoc($result);
		echo $langValue["res_value"];
	}
	else
	{
		//Create query
		$datetimenow=date('Y-m-d H:i:s');
		//create key for english language
		$queryInsertEU="INSERT INTO `pcshop`.`tbl_resource_language` (
            `resource_id` ,
            `resource_code` ,
            `res_key` ,
            `res_value` ,
            `res_module` ,
            `res_meta_field` ,
            `res_date_modify`
            )
            VALUES (
            NULL , 'en-US', '$key', '$key', '$pathFile' , 'na' , '$datetimenow'
            ) ";	
		$resultEU=mysql_query($queryInsertEU);
		if (!$resultEU) {
			$errors['error']='Invalid query: ' . mysql_error();
		}
		//create key for vietnamese language
		$queryInsertVI="INSERT INTO `pcshop`.`tbl_resource_language` (
            `resource_id` ,
            `resource_code` ,
            `res_key` ,
            `res_value` ,
            `res_module` ,
            `res_meta_field` ,
            `res_date_modify`
            )
            VALUES (
            NULL , 'vi-VN', '$key', '$key', '$pathFile' , 'na' , '$datetimenow'
            ) ";	
		$resultVI=mysql_query($queryInsertVI);
		if (!$resultVI) {
			$errors['error']='Invalid query: ' . mysql_error();
		}
		$query="SELECT `res_value` FROM `tbl_resource_language` WHERE `res_key`='$key' and `resource_code`='$get_current_lang'";
		$result=mysql_query($qry);
		if(mysql_num_rows($result) == 1)
		{
			$langValue = mysql_fetch_assoc($result);
			echo $langValue["res_value"];
		}

	}


}
/*
 * comment here
 */
function insertResourceLang($key,$val,$module,$langcode ){
	//Create query
	$datetimenow=date('Y-m-d H:i:s');
	$qry="INSERT INTO `pcshop`.`tbl_resource_language` (
            `resource_id` ,
            `resource_code` ,
            `res_key` ,
            `res_value` ,
            `res_module` ,
            `res_meta_field` ,
            `res_date_modify`
            )
            VALUES (
            NULL , '$langcode', '$key', '$val', '$module' , 'na' , '$datetimenow'
            ) ";
	$result=mysql_query($qry);
	if(!$result)
	{
		$errors['error']='Invalid query: ' . mysql_error();
		return false;
	}
	else {
		return true;
	}


}
/*
 * comment here
 */
function updateResourceLang($key,$val,$langcode){
	//Create query
	$datetimenow=date('Y-m-d H:i:s');
	$qry="SELECT `res_key` FROM `tbl_resource_language` WHERE `res_value`='$key'";
	$getResKey=mysql_query($qry);
	$result=mysql_fetch_assoc($getResKey);
	$res_key=$result["res_key"];
	$qry="UPDATE `tbl_resource_language` SET
        `res_value`='$val',`res_date_modify`='$datetimenow' WHERE  `resource_code`='$langcode' and  `res_key`='$res_key'";
	$result=mysql_query($qry);
	if(!$result)
	{
		$errors['error']='Invalid query: ' . mysql_error();
		return false;
	}
	else
	{
		return true;
	}

}
/*
 * comment here
 */
function getResourceLang($key){
	$data 			= array(); 		// array to pass back data
	//get res key
	$qryGetKey="SELECT `res_key` FROM `tbl_resource_language` WHERE `res_value`='$key'";
	$getResKey=mysql_query($qryGetKey);
	$resultKey=mysql_fetch_assoc($getResKey);
	$res_key=$resultKey["res_key"];
	//Create query
	$qry="SELECT `res_value` FROM `tbl_resource_language` WHERE `res_key`='$res_key' and `resource_code`='vi-VN'";
	$result=mysql_query($qry);
	if(!$result)
	{
		return $errors['error']='Invalid query: ' . mysql_error();
	}
	if(mysql_num_rows($result) >= 1)
	{
		$langValue = mysql_fetch_assoc($result);
		$data['vi']=$langValue["res_value"];
	}
	else
	{
		$data['vi']='resEmpty-VI';
	}
	$qry="SELECT `res_value` FROM `tbl_resource_language` WHERE `res_key`='$res_key' and `resource_code`='en-US'";
	$result=mysql_query($qry);
	if(!$result)
	{
		return $errors['error']='Invalid query: ' . mysql_error();
	}
	if(mysql_num_rows($result) >= 1)
	{
		$langValue = mysql_fetch_assoc($result);
		$data['en']=$langValue["res_value"];
	}
	else
	{
		$data['en']='resEmpty-US';
	}

	return $data;


}
/*
 * comment here
 */
function getResourceLangcode($key,$langcode){
	//Create query
	$qry="SELECT `res_value` FROM `tbl_resource_language` WHERE `res_key`='$key' and `resource_code`='$langcode'";
	$result=mysql_query($qry);
	if(!$result)
	{
		return $errors['error']='Invalid query: ' . mysql_error();
	}
	if(mysql_num_rows($result) >= 1)
	{
		$langValue = mysql_fetch_assoc($result);
		return  $data=$langValue["res_value"];
	}
	else
	{
		return 'resEmpty';
	}

}

/*
 * brief: check resource exits or not
 * param: $key
 * param: langcode
 */
function chekResourceExitsOrNot($key,$langcode){
	//get res key
	$qryGetKey="SELECT `res_key` FROM `tbl_resource_language` WHERE `res_value`='$key'";
	$getResKey=mysql_query($qryGetKey);
	$resultKey=mysql_fetch_assoc($getResKey);
	$res_key=$resultKey["res_key"];
	//Create query
	$qry="SELECT `res_value`,`res_key` FROM `tbl_resource_language`
	 WHERE `res_key`='$res_key' and `resource_code`='$langcode'";
	$result=mysql_query($qry);
	if(!$result)
	{
		return $errors['error']='Invalid query: ' . mysql_error();
	}
	if(mysql_num_rows($result) >= 1)
	{
		$langValue = mysql_fetch_assoc($result);
		$rdata['value']=$langValue["res_value"];
		$rdata['key']=$langValue["res_key"];
		return  $rdata;
	}
	else
	{
		return 'resEmpty';
	}

}
?>